package com.wpzf.system.controller;

import com.wpzf.common.core.utils.poi.ExcelUtil;
import com.wpzf.common.core.web.controller.BaseController;
import com.wpzf.common.core.web.domain.AjaxResult;
import com.wpzf.common.core.web.page.TableDataInfo;
import com.wpzf.common.log.annotation.Log;
import com.wpzf.common.log.enums.BusinessType;
import com.wpzf.common.security.annotation.RequiresPermissions;
import com.wpzf.common.security.utils.SecurityUtils;
import com.wpzf.system.domain.SysPlatform;
import com.wpzf.system.api.domain.SysUser;
import com.wpzf.system.domain.SysPlatformRole;
import com.wpzf.system.service.ISysPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 平台信息
 *
 * @author wpzf
 */
@RestController
@RequestMapping("/platform")
public class SysPlatformController extends BaseController {
    @Autowired
    private ISysPlatformService platformService;

    @RequiresPermissions("system:platform:list")
    @GetMapping("/list")
    public TableDataInfo list(SysPlatform platform) {
        startPage();
        List<SysPlatform> list = platformService.selectPlatformList(platform);
        return getDataTable(list);
    }

    @Log(title = "平台管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:platform:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPlatform platform) {
        List<SysPlatform> list = platformService.selectPlatformList(platform);
        ExcelUtil<SysPlatform> util = new ExcelUtil<SysPlatform>(SysPlatform.class);
        util.exportExcel(response, list, "平台数据");
    }

    /**
     * 根据平台编号获取详细信息
     */
    @RequiresPermissions("system:platform:query")
    @GetMapping(value = "/{platformId}")
    public AjaxResult getInfo(@PathVariable Long platformId) {
        return success(platformService.selectPlatformById(platformId));
    }

    /**
     * 新增平台
     */
    @RequiresPermissions("system:platform:add")
    @Log(title = "平台管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPlatform platform) {
        if (!platformService.checkPlatformNameUnique(platform)) {
            return error("新增平台'" + platform.getPlatformName() + "'失败，平台名称已存在");
        } else if (!platformService.checkPlatformKeyUnique(platform)) {
            return error("新增平台'" + platform.getPlatformName() + "'失败，平台权限已存在");
        }
        platform.setCreateBy(SecurityUtils.getUsername());
        return toAjax(platformService.insertPlatform(platform));
    }

    /**
     * 修改保存平台
     */
    @RequiresPermissions("system:platform:edit")
    @Log(title = "平台管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysPlatform platform) {
        if (!platformService.checkPlatformNameUnique(platform)) {
            return error("修改平台'" + platform.getPlatformName() + "'失败，平台名称已存在");
        } else if (!platformService.checkPlatformKeyUnique(platform)) {
            return error("修改平台'" + platform.getPlatformName() + "'失败，平台权限已存在");
        }
        platform.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(platformService.updatePlatform(platform));
    }

    /**
     * 状态修改
     */
    @RequiresPermissions("system:platform:edit")
    @Log(title = "平台管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysPlatform platform) {
        platform.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(platformService.updatePlatformStatus(platform));
    }

    /**
     * 删除平台
     */
    @RequiresPermissions("system:platform:remove")
    @Log(title = "平台管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{platformIds}")
    public AjaxResult remove(@PathVariable Long[] platformIds) {
        return toAjax(platformService.deletePlatformByIds(platformIds));
    }

    /**
     * 获取平台选择框列表
     */
    @RequiresPermissions("system:platform:query")
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        return success(platformService.selectPlatformAll());
    }

    @GetMapping("/userplatforms")
    public AjaxResult getPlatformByUser() {
        Long userId = SecurityUtils.getUserId();
        List<SysPlatformRole> list = platformService.getUserPlatformRoleByUserId(userId);
        return AjaxResult.success("平台信息获取成功", list);
    }

}
